package cn.com.cifi.mars.util;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class HttpClientWithBasicAuth {
	@Value("${api_name}")
	private String userName;
	
	@Value("${api_key}")
	private String pass;
	
	private HttpClientBuilder httpClientBuilder;
	
	private CloseableHttpClient closeableHttpClient;
	
	public String doGetMethod(String url,String filter) {
		if (closeableHttpClient == null) {
			createCloseableHttpClientWithBasicAuth();
		}
		HttpGet httpGet = new HttpGet(url.replace("FILTER", filter));
		String result = "";
		HttpResponse httpResponse = null;
		HttpEntity entity = null;
		try {
			httpResponse = closeableHttpClient.execute(httpGet);
			entity = httpResponse.getEntity();
			if (entity != null) {
				result = EntityUtils.toString(entity);
			}
		} catch (ClientProtocolException e) {
			log.error("###### errorMsg: ", e);
		} catch (IOException e) {
			log.error("###### errorMsg: ", e);
		}
		return result;
	}
	
	public String doPostMethod(String url,String content) {
		if (closeableHttpClient == null) {
			createCloseableHttpClientWithBasicAuth();
		}
		HttpPost post  =  new HttpPost(url);
		StringEntity stringEntity = new StringEntity(content,ContentType.create("text/json", "UTF-8"));
		post.setEntity(stringEntity);
		HttpResponse response = null;
		HttpEntity entity = null;
		String result = "";
		try {
			response = closeableHttpClient.execute(post);
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				entity = response.getEntity();
				if (entity != null) {
					result = EntityUtils.toString(entity);
					result = new String(result.getBytes("iso-8859-1"), "utf-8");
				}
		    }
		} catch (Exception e) {
			log.error("###### errorMsg: ", e);
		} 
		return result;
	}
	

	public void closeHttpClient() {
		if (closeableHttpClient != null) {
			try {
				closeableHttpClient.close();
			} catch (IOException e) {
				log.error("###### errorMsg: ", e);
			}
		}
	}

	public void createCloseableHttpClient() {
		if (closeableHttpClient == null) {
			httpClientBuilder = HttpClientBuilder.create();
			closeableHttpClient = httpClientBuilder.build();
		}
	}

	public void createCloseableHttpClientWithBasicAuth() {
		if (closeableHttpClient == null) {
			httpClientBuilder = HttpClientBuilder.create();
			CredentialsProvider provider = new BasicCredentialsProvider();
			AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM);
			UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(userName, pass);
			provider.setCredentials(scope, credentials);
			httpClientBuilder.setDefaultCredentialsProvider(provider);
			closeableHttpClient = httpClientBuilder.build();
		}
	}

/*	public static void main(String args[]) throws UnsupportedEncodingException {
		HttpClientWithBasicAuth httpClient = new HttpClientWithBasicAuth();
		String url = "http://esb-uat.cifi.com.cn:8085/v1/idm/users/query?filter=FILTER";
		String filter="(usertype=E1)&pageSize=2";
		String result = httpClient.doGetMethod(url,filter);
		result = new String(result.getBytes("iso-8859-1"), "utf-8");
		
		String url = "http://esb-uat.cifi.com.cn:8081/v1/esb/signPlan/publish";
		
		String result = httpClient.doPostMethod(url, "");
		log.info(result);
	}*/
}
